<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp" %>
<%@include file="/common/javascript.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>资源</title>
    <link rel="stylesheet" href="${ctx}/static/ztree/css/demo.css" type="text/css" />
	<link rel="stylesheet" href="${ctx}/static/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
    <script type="text/javascript">
    var zTree;
    var mainform;
    var viewType = "${viewType}";
	var setting = {
			check: {enable: true,chkStyle: "radio",radioType: "all"},
			view: {dblClickExpand: false},
			data: {simpleData: {enable: true}},
			async : {enable : true,url : "${ctx}/menu/menuTree"},
			callback: {onClick: onClick,onCheck: onCheck,onNodeCreated: nodeCreated}
		};
	
        $(function ()
        {        
        	zTree=$.fn.zTree.init($("#treeDemo"), setting);
        	
            mainform=$("#mainform");
            
            mainform.ligerForm();
            
            if(viewType=="modify"){
            	 $("#rscId").attr("readonly", "readonly").removeAttr("validate");
            }
            
            if(viewType=="detail"){
            	$("input,select,textarea",mainform).attr("readonly", "readonly");
            }else{
                $.metadata.setType("attr", "validate");
                LG.validate(mainform);
            }
            
            LG.setFormDefaultBtn(cancel,viewType=="detail" ? null : save);

        });  
        
        function save(){
            LG.submitForm(mainform, function (result) {
            	if(typeof(result)=="string"){
            		result=eval("("+result+")");
            	}
                if (!result.success) {
                    LG.showError('错误:' + data.message);
                }
                else { 
                    LG.showSuccess('保存成功', function () { 
                       LG.callParent("resourceList");
                    });
                }
            });
        }
        function cancel(){
            var win = window.parent || window;
            if(win.dialog){
            	win.dialog.close();
            }
        }
        
    	function nodeCreated(event, treeId, treeNode) {
    		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	    	if(treeNode.id==$("#muId").val()){
	    		treeNode.checked=true;
	    		zTree.updateNode(treeNode,true);
	    	}
    	}
    	
		function onClick(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.checkNode(treeNode, !treeNode.checked, null, true);
			return false;
		}

		function onCheck(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getCheckedNodes(true);
			if (nodes.length > 0 ){
				var node=nodes[0];
				$("#muName").val(node.name);
				$("#muId").val(node.id);
			}
		}

		function showMenu() {
			var cityObj = $("#muName");
			var cityOffset = $("#muName").offset();
			$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
			$("body").bind("mousedown", onBodyDown);
		}
		function hideMenu() {
			$("#menuContent").fadeOut("fast");
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "muName" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
				hideMenu();
			}
		}
    </script>
</head>

<body style="padding:10px">
    <form name="mainform" method="post"  id="mainform" action="${ctx }/resource/save">
    	<input type="hidden" name="viewType" value="${viewType}"/>
        <table cellpadding="0" cellspacing="0" class="l-table-edit" style="width: 100%;">
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">资源代码:</td>
                <td align="left" class="l-table-edit-td" style="width:80px" >
                	<input type="text"  name="rscId" id="rscId" value="${command.rscId }" ltype="text" validate="{required:true,minlength:3,maxlength:50,notnull:true,remote:'${ctx}/resource/checkRscId?oldRscId=${command.rscId}'}"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">资源名称:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">
	               <input type="text"  name="rscName" id="rscName" value="${command.rscName}" ltype="text" validate="{required:true,minlength:3,maxlength:50,notnull:true}"/>
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">资源地址:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">
                	<input type="text"  name="rscUri" id="rscUri" value="${command.rscUri}" ltype="text" validate="{required:true,minlength:3,maxlength:50,notnull:true}"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">权限表达式:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">
                	<input type="text" name="rscPerm" id="rscPerm" value="${command.rscPerm}" ltype="text" validate="{required:true,notnull:true}"/>
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">菜单:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">
                	<input type="hidden" name="menu.muId" id="muId" value="${command.menu.muId}"/>
                	<input type="text" readonly="readonly" name="muName" id="muName" value="${command.menu.muName }" validate="{required:true,notnull:true}" onclick="showMenu();"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top"></td>
                <td align="left" class="l-table-edit-td" style="width:80px">
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td">备注:</td>
                <td align="left" class="l-table-edit-td"> 
                <textarea cols="100" rows="3" class="l-textarea" style="width:400px" id="remark" name="remark" >${command.remark}</textarea>
                </td> <td align="left"></td>
           </tr>
           <tr>
                <td align="left" class="l-table-edit-td" valign="top">建档人:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">${command.userCre}</td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">修改人:</td>
                <td align="left" class="l-table-edit-td" style="width:80px">${command.userUpd}</td>
                <td align="left"></td>
           </tr>
        </table>
    </form>
    
<div id="menuContent" class="menuContent" style="display:none; position: absolute;">
	<ul id="treeDemo" class="ztree" style="margin-top:0; width:180px; height: 300px;"></ul>
</div>
</body>
</html>
